﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace LiveEduWizard
{
    public class TimetableViewModel : INotifyPropertyChanged
    {
        public TimetableViewModel()
        {
            Monday = new ObservableCollection<AppointmentViewModel>(); 
            Tuesday = new ObservableCollection<AppointmentViewModel>();
            Wednesday = new ObservableCollection<AppointmentViewModel>();
            Thursday = new ObservableCollection<AppointmentViewModel>();
            Friday = new ObservableCollection<AppointmentViewModel>();
        }

        public ObservableCollection<AppointmentViewModel> Monday { get; private set; }
        public ObservableCollection<AppointmentViewModel> Tuesday { get; private set; }
        public ObservableCollection<AppointmentViewModel> Wednesday { get; private set; }
        public ObservableCollection<AppointmentViewModel> Thursday { get; private set; }
        public ObservableCollection<AppointmentViewModel> Friday { get; private set; }

        private int week;
        public int Week
        {
            get
            {
                return week;
            }
            set
            {
                if (value != week)
                {
                    week = value;
                    NotifyPropertyChanged("Week");
                    NotifyPropertyChanged("WeekName");
                }
            }
        }

        public string WeekName
        {
            get
            {
                string num;

                switch (week)
                {
                    case 1:
                        num = "st";
                        break;
                    case 2:
                        num = "nd";
                        break;
                    case 3:
                        num = "rd";
                        break;
                    default:
                        num = "th";
                        break;
                }

                return string.Format("{0}{1} week", week, num);
            }
        }

        private int year;
        public int Year
        {
            get
            {
                return year;
            }
            set
            {
                if (value != year)
                {
                    year = value;
                    NotifyPropertyChanged("Year");
                }
            }
        }

        public bool IsActive
        {
            get { return App.ViewModel.ActiveTimetable == this; }
        }

        public bool IsNotActive
        {
            get { return !IsActive; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        internal bool TryAdd(AppointmentViewModel appointment)
        {
            if (appointment.WeekYear == year
                && appointment.Week == week)
            {
                Add(appointment);
                return true;
            }
            else
                return false;
        }

        private void Add(AppointmentViewModel appointment)
        {
            switch (appointment.Start.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    Monday.Add(appointment);
                    break;
                case DayOfWeek.Tuesday:
                    Tuesday.Add(appointment);
                    break;
                case DayOfWeek.Wednesday:
                    Wednesday.Add(appointment);
                    break;
                case DayOfWeek.Thursday:
                    Thursday.Add(appointment);
                    break;
                case DayOfWeek.Friday:
                    Friday.Add(appointment);
                    break;
            }
        }

        internal static TimetableViewModel Create(AppointmentViewModel firstAppointment)
        {
            var timetable = new TimetableViewModel();
            
            timetable.year = firstAppointment.WeekYear;
            timetable.week = firstAppointment.Week;

            timetable.Add(firstAppointment);

            return timetable;
        }
    }
}
